package com.supor.aiot.basket.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alipay.android.phone.mobilecommon.multimediabiz.biz.falcon.interf.IFalconService;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.supor.aiot.basket.R;
import com.supor.aiot.basket.bean.IngredientBean;
import com.supor.aiot.basket.bean.IsMainIngredients;
import com.supor.aiot.basket.bean.OutTagBean;
import com.supor.aiot.basket.bean.SelectedTextBean;

import java.util.ArrayList;
import java.util.List;


public class BasketOuterAdapter extends RecyclerView.Adapter<BasketOuterAdapter.BasketHolder>{
    private IngredientBean ingredientBean;
    private Context mContext;
    private List<OutTagBean> outTagBeans;

    public BasketOuterAdapter(IngredientBean bean, Context mContext) {
        this.ingredientBean = bean;
        this.mContext = mContext;
        initTagBean(ingredientBean);
    }

    private void initTagBean(IngredientBean bean){
        outTagBeans = new ArrayList<>();
        if (bean.getIsMainIngredients()!=null&&bean.getIsMainIngredients().size()>0){
            OutTagBean outTagBean = new OutTagBean();
            outTagBean.setTagTitle(mContext.getString(R.string.main_ingredients));
            List<SelectedTextBean> mainIngredient = new ArrayList<>();
            for (int i=0;i<bean.getIsMainIngredients().size();i++){
                IsMainIngredients isMainIngredients = bean.getIsMainIngredients().get(i);
                mainIngredient.add(new SelectedTextBean(isMainIngredients.getIngredientName(),isMainIngredients.getIsSelect()==1?true:false));
            }
            outTagBean.setTags(mainIngredient);
            outTagBeans.add(outTagBean);
        }

        if (bean.getIsTsIngredients()!=null&&bean.getIsTsIngredients().size()>0){
            OutTagBean outTagBean = new OutTagBean();
            outTagBean.setTagTitle(mContext.getString(R.string.other_ingredients));
            List<SelectedTextBean> mainIngredient = new ArrayList<>();
            for (int i=0;i<bean.getIsTsIngredients().size();i++){
                IsMainIngredients isMainIngredients = bean.getIsTsIngredients().get(i);
                mainIngredient.add(new SelectedTextBean(isMainIngredients.getIngredientName(),isMainIngredients.getIsSelect()==1?true:false));
            }
            outTagBean.setTags(mainIngredient);
            outTagBeans.add(outTagBean);
        }

        if (bean.getIsCgIngredients()!=null&&bean.getIsCgIngredients().size()>0){
            OutTagBean outTagBean = new OutTagBean();
            outTagBean.setTagTitle(mContext.getString(R.string.cgingredients));
            List<SelectedTextBean> mainIngredient = new ArrayList<>();
            for (int i=0;i<bean.getIsCgIngredients().size();i++){
                IsMainIngredients isMainIngredients = bean.getIsCgIngredients().get(i);
                mainIngredient.add(new SelectedTextBean(isMainIngredients.getIngredientName(),isMainIngredients.getIsSelect()==1?true:false));
            }
            outTagBean.setTags(mainIngredient);
            outTagBeans.add(outTagBean);
        }
    }

    @NonNull
    @Override
    public BasketOuterAdapter.BasketHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_hide_desc,parent, false);
        return new BasketOuterAdapter.BasketHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BasketHolder holder, int position) {

        holder.tvHideName.setText(outTagBeans.get(position).getTagTitle());
        BasketTextAdapter adapter = new BasketTextAdapter(outTagBeans.get(position).getTags(),mContext);
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(mContext);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        layoutManager.setFlexWrap(FlexWrap.WRAP);//按正常方向换行
        holder.rvHideIngredient.setAdapter(adapter);
        holder.rvHideIngredient.setLayoutManager(layoutManager);
    }



    @Override
    public int getItemCount() {
        return outTagBeans.size();
    }

    public class BasketHolder extends RecyclerView.ViewHolder{
        private TextView tvHideName;
        private RecyclerView rvHideIngredient;

        public BasketHolder(@NonNull View itemView) {
            super(itemView);
            tvHideName = itemView.findViewById(R.id.tv_hide_name);
            rvHideIngredient = itemView.findViewById(R.id.rv_hide_ingredient);
        }
    }


}
